package com.bkitmobile.kesu.android.ClassInfo;

import java.io.ByteArrayInputStream;
import java.util.List;

import com.bkitmobile.kesu.android.GUI.R;
import com.bkitmobile.kesu.android.GUI.R.drawable;
import com.bkitmobile.kesu.android.GUI.R.id;
import com.bkitmobile.kesu.android.GUI.R.layout;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendsListAdapter extends ArrayAdapter<FriendShortInfo>{
	private Activity context;
	private FriendShortInfo []friendShortInfos;
	private List<FriendShortInfo> listFriendShortInfo;
	public FriendsListAdapter(Activity context, FriendShortInfo[] objects) {
		super(context, R.layout.friend__friend_object, objects);
		this.context = context;
		friendShortInfos = objects;
		listFriendShortInfo = null;
	}
	
	public FriendsListAdapter(Activity context, List<FriendShortInfo> objects){
		super(context, R.layout.friend__friend_object, objects);
		this.context = context;
		friendShortInfos = null;
		listFriendShortInfo = objects;
		
	}
	public View getView(int position,  View convertView, ViewGroup parent) {
		View row = convertView;
		if(row == null){
			LayoutInflater inflater = context.getLayoutInflater();
			row = inflater.inflate(R.layout.friend__friend_object, null);
		}
		
		ImageView avatar = (ImageView) row.findViewById(R.id.friendObject_avatar);
		TextView id = (TextView) row.findViewById(R.id.friendObject_name);
		TextView status = (TextView) row.findViewById(R.id.friendObject_status);
		FriendShortInfo object = getAnFriendShortInfo(position);
		if(object != null){
			id.setText(object.getId());
			status.setText(object.getStatus());
			if(object.isState()){
				//TODO: load image to avatar
				try {
					String str = object.getImg();
					byte[] bytes = Base64.decode(str, Base64.DEFAULT);
					ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
					BitmapDrawable bm = new BitmapDrawable(baos);
					Bitmap bitmapResourse = BitmapFactory.decodeResource(context.getResources(), R.drawable.online);
					Bitmap av = Bitmap.createScaledBitmap(bm.getBitmap(), bitmapResourse.getWidth()
							, bitmapResourse.getHeight(), false);
					avatar.setImageBitmap(av);
				} catch (Exception e){
					avatar.setImageResource(R.drawable.online);
				}
			}
			else{
				avatar.setImageResource(R.drawable.offline);
			}
		}
		return row;
	}
	public List<FriendShortInfo> getAllInfoList()
	{	
		return listFriendShortInfo;
	}
	private FriendShortInfo getAnFriendShortInfo(int position){
		if(friendShortInfos == null && listFriendShortInfo == null)
			return null;
		FriendShortInfo result;
		if(friendShortInfos == null){
			if(position < listFriendShortInfo.size())
				result = listFriendShortInfo.get(position);
			else
				result = null;
		}
		else{
			if(position < friendShortInfos.length)
				result = friendShortInfos[position];
			else
				result = null;
		}
			
		return result;
	}

}

